//
//  FloatLayerView.m
//  Test
//
//  Created by sheng long on 14-6-28.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "FloatLayerView.h"

#define IMAGE_HEIGHT     6
#define X_GAP           8

#define RHOMBUS_WIDTH   7.0
//#define RHOMBUS_HEIGHT  13.86
#define RHOMBUS_HEIGHT  12.0

typedef enum {
    NORMAL_ROUND_STATE = 1210,
    LEFT_ROUND_STATE,
    RIGHT_ROUND_STATE,
    NO_ROUND_STATE
}HomeFloatRoundState;

@implementation FloatLayerView

@synthesize contentText,contentArray,numberArray;

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

    }
    return self;
}

-(void)drawRect:(CGRect)rect
{
    // 获取CGContext，注意UIKit里用的是一个专门的函数
    CGContextRef context = UIGraphicsGetCurrentContext();
    CLog(@"%d",contentArray.count);
    
    CGFloat width = (self.frame.size.width-X_GAP*2)/contentArray.count;

    for (int i = 0 ; i < contentArray.count; i++) {
        HomeFloatRoundState state;
        if (i == 0 && contentArray.count==1) {
            state = NORMAL_ROUND_STATE;
        }else if(i == 0){
            state = LEFT_ROUND_STATE;
        }else if(i == contentArray.count-1){
            state = RIGHT_ROUND_STATE;
        }else{
            state = NO_ROUND_STATE;
        }
        
        NSDictionary *dic = [contentArray objectAtIndex:i];
        CGRect roundRect = CGRectMake(X_GAP+width*i, 35, width, IMAGE_HEIGHT);
        [self drawRect:context rect:roundRect closeState:state color:[dic objectForKey:@"color"]];
        
        NSString *text = [dic valueForKey:@"text"];
        if (text) {
            CGSize size = [z_UI returnHeightFloat:text frontSize:FLOATLAYERVIEW_TEXT_FONT frontWidth:rect.size.width];
            [text drawInRect:CGRectMake(roundRect.origin.x+roundRect.size.width/2-size.width/2, roundRect.size.height+roundRect.origin.y+5, size.width, size.height) withFont:FLOATLAYERVIEW_TEXT_FONT];
        }
        
        if (numberArray && i>0) {
            CGPoint points[4];
            points[0] = CGPointMake(roundRect.origin.x, roundRect.origin.y+roundRect.size.height+5);
            points[1] = CGPointMake(points[0].x-RHOMBUS_WIDTH/2, points[0].y+RHOMBUS_HEIGHT/2);
            points[3] = CGPointMake(points[0].x+RHOMBUS_WIDTH/2, points[0].y+RHOMBUS_HEIGHT/2);
            points[2] = CGPointMake(points[0].x, points[0].y+RHOMBUS_HEIGHT);
            [self drawRect:context points:points number:[numberArray objectAtIndex:(i-1)]];
        }
    }
    
    if (contentText) {
        CGSize size = [z_UI returnHeightFloat:contentText frontSize:FLOATLAYERVIEW_TEXT_FONT frontWidth:self.frame.size.width-X_GAP*2];
        CGContextSetFillColorWithColor(context, GET_COLOR(@"d3d3d3").CGColor);
        [contentText drawInRect:CGRectMake(X_GAP, FLOATLAYERVIEW_IMGAE_HEIGHT, size.width, size.height) withFont:FLOATLAYERVIEW_TEXT_FONT];
    }
}

//画圆角矩形
-(void)drawRect:(CGContextRef)context rect:(CGRect)rect closeState:(HomeFloatRoundState)state color:(UIColor*)color
{
    /*画圆角矩形*/
    CGFloat x = rect.origin.x;
    CGFloat y = rect.origin.y;
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    CGFloat radius = height/2;
    
    CGFloat leftRadius;
    CGFloat rightRadius;
    
    switch (state) {
        case NORMAL_ROUND_STATE:
        {
            leftRadius = radius;
            rightRadius = radius;
        }
            break;
        case LEFT_ROUND_STATE:
        {
            leftRadius = radius;
            rightRadius = 0;
        }
            break;
        case RIGHT_ROUND_STATE:
        {
            leftRadius = 0;
            rightRadius = radius;
        }
            break;
        case NO_ROUND_STATE:
        {
            leftRadius = 0;
            rightRadius = 0;
        }
            break;
        default:
            break;
    }
    
    // 开始坐标右边开始
    CGContextMoveToPoint(context, x+width, 0);
    
    // 右下角角度
    CGContextAddArcToPoint(context, x+width, y+height, x+width-height, y+height, rightRadius);
    
    // 左下角角度
    CGContextAddArcToPoint(context, x, y+height, x, y+height-radius, leftRadius);
    
    // 左上角
    CGContextAddArcToPoint(context, x, y, x+width-height, y, leftRadius);
    
    // 右上角
    CGContextAddArcToPoint(context, x+width, y, x+width, y+height-radius, rightRadius);
    
    // 闭合路径
    CGContextClosePath(context);
    // 填充半透明黑色
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextDrawPath(context, kCGPathFill); //根据坐标绘制路径
}

//画标识菱形
-(void)drawRect:(CGContextRef)context points:(CGPoint[])points number:(NSString*)number
{
    number = [NSString stringWithFormat:@"%.1f",number.floatValue];
    
    //只要三个点就行跟画一条线方式一样，把三点连接起来
    CGContextAddLines(context, points, 4);//添加线
    CGContextClosePath(context);//封起来
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextDrawPath(context, kCGPathFill); //根据坐标绘制路径
    
    if (number) {
        UIFont *font = [UIFont systemFontOfSize:8];
        CGSize size = [z_UI returnHeightFloat:number frontSize:font frontWidth:100];
        CGContextSetFillColorWithColor(context, GET_COLOR(@"d3d3d3").CGColor);
        [number drawInRect:CGRectMake(points[0].x-size.width/2, points[2].y+3, size.width, size.height) withFont:font];
    }
}

@end
